//jqGrid的配置信息
$.jgrid.defaults.width = 1000;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';

var baseURL = "../../";

//工具集合Tools
window.T = {};

// 获取请求参数
// 使用示例
// location.href = http://localhost:8080/index.html?id=123
// T.p('id') --> 123;
var url = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
};
T.p = url;

//全局配置
$.ajaxSetup({
    dataType: "json",
    cache: false
});

//重写alert
window.alert = function (msg, callback) {
    parent.layer.alert(msg, function (index) {
        parent.layer.close(index);
        if (typeof (callback) === "function") {
            callback("ok");
        }
    });
}

//重写confirm式样框
window.confirm = function (msg, callback) {
    parent.layer.confirm(msg, {btn: ['确定', '取消']},
        function () {//确定事件
            if (typeof (callback) === "function") {
                callback("ok");
            }
        });
}

//选择一条记录
function getSelectedRow() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if (!rowKey) {
        alert("请选择一条记录");
        return;
    }

    var selectedIDs = grid.getGridParam("selarrrow");
    if (selectedIDs.length > 1) {
        alert("只能选择一条记录");
        return;
    }

    return selectedIDs[0];
}

//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if (!rowKey) {
        alert("请选择一条记录");
        return;
    }

    return grid.getGridParam("selarrrow");
}

//判断是否为空
function isBlank(value) {
    return !value || !/\S/.test(value)
}

/**
 * 获取当前用户
 */
function getuser() {
    var user;
    $.ajax({
        type: "get",
        url: "/sys/user/getuser",
        cache: false,
        async: false,
        success: function (r) {
            user = r.user;
        }
    });
    return user;
}

/**
 * 根据部门获取用户
 */
function getuserlist(deptId) {
    var dept;
    $.ajax({
        type: "get",
        url: "/sys/user/lists?deptId=" + deptId+"",
        cache: false,
        async: false,
        success: function (r) {
            dept = r.page;
        }

    });
    return dept;
}

/**
 * 年份
 * @param deptId
 * @returns {*}
 */

function inityear() {
    var dept;
    $.ajax({
        type: "get",
        url: "/sys/tianan/listyear",
        cache: false,
        async: false,
        success: function (r) {
            dept =r.year;
        }

    });
    return dept;
}
/**
 * 根据类型获取用户
 */
function getuserlisttype(type) {
    var dept;
    $.ajax({
        type: "get",
        url: "/sys/user/lists?type=" + type,
        cache: false,
        async: false,
        success: function (r) {
            dept = r.page;
        }

    });
    return dept;
}

/**
 * 所有部门
 */
function getDeptAll(deptId) {
    var dept;
    $.ajax({
        type: "get",
        url: "/sys/dept/list?deptId=" + deptId,
        cache: false,
        async: false,
        success: function (r) {
            dept = r;
        }
    });
    return dept;
}
/**
 * 所有选区
 */
function getxuanqulist() {
    var dept;
    $.ajax({
        type: "get",
        url: "/sys/xuanqu/lists",
        cache: false,
        async: false,
        success: function (r) {
            dept = r.page;
        }

    });
    return dept;
}
/**
 * 所有部门
 */
function getDeptAlls(deptId) {
    var dept;
    $.ajax({
        type: "get",
        url: "/sys/dept/lists?deptId=" + deptId,
        cache: false,
        async: false,
        success: function (r) {
            dept = r;
        }
    });
    return dept;
}

/**
 * 字典查看
 */
function getDict(deptId) {
    var dept;
    $.ajax({
        type: "get",
        url: "/sys/dict/list?type=" + deptId,
        cache: false,
        async: false,
        success: function (r) {
            dept = r.page.list;
        }

    });
    return dept;
}

/**
 * 参数获取
 */
function getconfig(id) {
    var dept;
    $.ajax({
        type: "get",
        url: "/sys/config/getvalue/" + id,
        cache: false,
        async: false,
        success: function (r) {
            dept = r.data;
        }

    });
    return dept;
}

/**
 * 立案类型
 * */
function lian(row) {
    var taskstate;
    switch (row) {

        case '1':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:green' aria-hidden='true'>立案</i>";
            break;
        case '2':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:green' aria-hidden='true'>解释</i>";
            break;
        case '3':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:#FF5722' aria-hidden='true'>不予立案</i>";
            break;
        default:
            taskstate = "<i class='glyphicon glyphicon-time' style='color:#ffd306' aria-hidden='true'>待立案</i>";
            break;
    }
    return taskstate;
}

/**
 * 立案状态 1已办理待部门受理 ，0 未处理  2部门以受理，3退回
 * */
function lianstatus(row) {
    var taskstate;
    switch (row) {
        case '0':
            taskstate = "<i class='glyphicon glyphicon-time' style='color:#ffd306' aria-hidden='true'>待立案</i>";
            break;
        case '1':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:#ffd306' aria-hidden='true'>待部门受理</i>";
            break;
        case '2':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:green' aria-hidden='true'>部门已受理</i>";
            break;
        case '3':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:red' aria-hidden='true'>部门退回</i>";
            break;

    }
    return taskstate;
}

/**
 * 预审状态
 * */
function yushenstatus(row) {
    var taskstate;
    switch (row) {
        case '-1':
            taskstate = "<i class='glyphicon glyphicon-time' style='color:#ffd306' aria-hidden='true'>待预审</i>";
            break;
        case '0':
            taskstate = "<i class='glyphicon glyphicon-time' style='color:#ffd306' aria-hidden='true'>待预审</i>";
            break;

        case '1':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:green' aria-hidden='true'>已预审</i>";
            break;
    }
    return taskstate;
}

/**
 * 审核状态
 * */
function checkstatus(row) {
    var taskstate;
    switch (row) {
        case '1':
            taskstate = "<i class='glyphicon glyphicon-time' style='color:#FF5722' aria-hidden='true'>未处理</i>";
            break;
        case '2':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:#1E9FFF' aria-hidden='true'>经办人处理中</i>";
            break;
        case '3':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:#FFB800' aria-hidden='true'>待部门负责人审核</i>";
            break;
        case '-3':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:red' aria-hidden='true'>部门负责人审核未通过</i>";
            break;
        case '4':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:#FFB800' aria-hidden='true'>部门负责人审核通过，待分管院领导审批</i>";
            break;
        case '-4':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:red' aria-hidden='true'>分管院领导审批未通过</i>";
            break;
        case '5':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:#FFB800' aria-hidden='true'>已办理，待评价</i>";
            break;
        case '6':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:green' aria-hidden='true'>已办结</i>";
            break;
        case '-1':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:red' aria-hidden='true'>超时未处理</i>";
            break;

        default:
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:#FFB800' aria-hidden='true'>未开始</i>";
    }
    return taskstate;
}

/**
 * 附议状态
 * */
function fuyistatus(row) {
    var taskstate;
    switch (row) {
        case '-1':
            taskstate = "<i class='glyphicon glyphicon-time' style='color:red' aria-hidden='true'>附议人不通过</i>";
            break;
        case '0':
            taskstate = "<i class='glyphicon glyphicon-time' style='color:#ffd306' aria-hidden='true'>附议人查看中</i>";
            break;

        case '1':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:green' aria-hidden='true'>附议人以通过</i>";
            break;

    }
    return taskstate;
}

/**
 *
 满意度
 * */
function manyistatus(row) {
    var taskstate;
    switch (row) {
        case '3':
            taskstate = "<i class='glyphicon glyphicon-time' style='color:red' aria-hidden='true'>不满意</i>";
            break;
        case '2':
            taskstate = "<i class='glyphicon glyphicon-time' style='color:#ffd306' aria-hidden='true'>基本满意</i>";
            break;

        case '1':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:green' aria-hidden='true'>满意</i>";
            break;
        default:
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:#1E9FFF' aria-hidden='true'>未评价</i>";
            break;

    }
    return taskstate;
}

/**
 * 联名人审核状态
 * */
function lianmingtatus(row) {
    var taskstate;
    switch (row) {
        case '0':
            taskstate = "<i class='glyphicon glyphicon-time' style='color:#ffd306' aria-hidden='true'>联名人查看中</i>";
            break;
        case '2':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:red' aria-hidden='true'>联名人不同意</i>";
            break;
        case '1':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:green' aria-hidden='true'>联名人已通过</i>";
            break;
    }
    return taskstate;
}


/**
 * 签发意见
 * */
function yijian(row) {
    var taskstate;
    switch (row) {
        case '2':
            taskstate = "<i class='glyphicon glyphicon-time' style='color:#ffd306' aria-hidden='true'>其他意见</i>";
            break;
        case '3':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:red' aria-hidden='true'>不同意</i>";
            break;
        case '1':
            taskstate = "<i class='glyphicon glyphicon-ok-circle' style='color:green' aria-hidden='true'>同意</i>";
            break;
    }
    return taskstate;
}

/**
 * 提案类别
 * */
function tianleibie(value) {
    var taskstate;
    switch (value) {
        case '1':
            taskstate = "个人";
            break;
        case '2':
            taskstate = "联名";
            break;
        case '3':
            taskstate = "代表组";
            break;
    }
    return taskstate;
}

/**
 * 立案类别
 * */
function liantype(value) {
    var taskstate;
    switch (value) {
        case '1':
            taskstate = "立案";
            break;
        case '2':
            taskstate = "解释";
            break;
        case '3':
            taskstate = "不立案";
            break;
        default :
            taskstate = "未上会";

    }
    return taskstate;
}


var downUrl = [];
var title;
function info(id) {
    var tianan;
    $.ajax({
        type: "GET",
        url: baseURL + "../sys/tianan/info/" + id,
        cache: false,
        async: false,
        success: function (r) {
            tianan = r.tianan;
            var lianstatu;
            if (tianan.lianstatus == 2||tianan.lianstatus == '2') {
                lianstatu = checkstatus(tianan.banjiestatus)
            } else {
                lianstatu = lianstatus(tianan.lianstatus)

            }
            if(tianan.liantype==3){

                lianstatu = '<span style="color: red">终止</span>';
            }
            if (tianan.banjiestatus<5) {
                tianan.subexplain='已回复，待审核通过即可查看';
                tianan.subfile='';

            }

            layer.open({
                title: tianan.title,
                type: 1,
                skin: 'layui-layer-molv',
                area: ['800px', '450px'],
                content: '<div class="row" style="padding: 10px">' +
                    ' <table class="table table-bordered table-style"> ' +
                    ' <tbody> ' +
                    '<tr><td style="width:110px; ">提案人</td><td style="    width: 140px;">' + isnull(tianan.creatusername) + '</td><td style="width:85px;text-align: center; ">提案类别</td><td>' + isnull(tianan.typesName) + '</td><td style="width:85px;text-align: center; ">所在部门</td><td>' + isnull(tianan.deptIdName) + '</td></tr>' +
                    '<tr><td>案由分析</td><td colspan="5">' + isnull(tianan.anyoufenxi) + '</td></tr>' +
                    '<tr><td>措施建议</td><td colspan="5">' + isnull(tianan.cuoshijianyi) + '</td></tr>' +
                    '<tr><td>办理时限</td><td >' + isnull(tianan.endtime) + '</td><td style="width:85px;text-align: center;">办理类型</td><td>' + isnull(liantype(tianan.liantype)) + '</td><td style="width:85px;text-align: center; ">办理部门</td><td>' + isnull(tianan.liandeptname) + '</td></tr>' +
                    '<tr><td>提案类型</td><td >' + isnull(tianleibie(tianan.type)) + '</td><td style="width:85px;text-align: center;">办理状态</td><td>' + lianstatu + '</td><td style="width:85px;text-align: center; ">提案人满意度</td><td>' + isnull(manyistatus(tianan.manyi)) + '</td></tr>' +
                    '<tr><td>提案答复</td><td colspan="5">' + isnull(tianan.subexplain) + '</td></tr>' +
                    '<tr><td >经办人</td><td >' +  isnull(tianan.jingbanusername) +  '</td><td>部门负责人审核时间</td><td >' + isnull(tianan.restime) + '</td><td>部门负责人意见 </td><td>' + isnull(tianan.rescause) + '</td></tr>' +
                    '<tr><td >分管院领导审核时间</td><td colspan="2">' +isnull(tianan.ledtime) + '</td><td>分管院领导意见</td><td  colspan="3">' + isnull(tianan.leacause) + '</td></tr>' +
                    ' </tbody> ' +
                    '</table>' +
                    '<div class="input-group ">相关附件：<ul id="ulli"></ul></div>' +
                    ' </div>',
                success: function () {

                    if (tianan.subfile != null && tianan.subfile != '') {
                        var s = [];
                        s = tianan.subfile.split(",");
                        //遍历所有文件
                        for (var i = 0; i < s.length; i++) {
                            if (s[i] != null && s[i] != 'null') {
                                s[i].replace("/", "\\")
                            }
                            var fileNameIndex = s[i].lastIndexOf("\\");
                            var filePathName = s[i].substring(fileNameIndex + 1);
                            var html = filePathName;
                            downUrl.push(s[i]);

                            html = '<li>' +
                                html + '<a class="operation_type" style="color: #0c74c4; text-decoration:none" href="../../../sys/file/fileDown?fileDir=' + encodeURIComponent(s[i]) + '"  > &nbsp;&nbsp;下载</a>'
                                + "<a class='operation_type'  style='color: #0c74c4;  border:none;text-decoration:none'   onclick='eye( \"" + s[i].replace(/\\/g, "//") + "\")' href='javascript:;'  > &nbsp;&nbsp;预览</a></li>";
                            $("#ulli").append(html);
                        }
                    } else {
                        $("#ulli").append("<p>无</p>");
                    }
                }
            })
        }
    })
}

function getPreview(path) {
    parent.layer.open({
        type: 2,
        title: '预览页',
        shadeClose: true,
        area: ['70%', '90%'],
        content: path  //iframe的url
    });
}

/**
 * 预览文件
 * @param event
 */
function eye(event) {
    window.sessionStorage.setItem("file", event);
    var num1 = event.lastIndexOf(".");
    if (event.substring(num1).toLowerCase() == ".pdf") {
        getPreview("/sys/file/devDocpath?path=" + encodeURIComponent(event))
    } else if (event.substring(num1).toLowerCase() == ".xls" || event.substring(num1).toLowerCase() == ".xlsx") {
        getPreview("/sys/view/compWordpdfs?path=" + encodeURIComponent(event))
    } else if (event.substring(num1).toLowerCase() == ".doc" || event.substring(num1).toLowerCase() == ".docx") {
        getPreview("/sys/view/compWordpdfs?path=" + encodeURIComponent(event))
    } else if (event.substring(num1).toLowerCase() == ".ppt" || event.substring(num1).toLowerCase() == ".pptx") {
        getPreview("/sys/view/compWordpdfs?path=" + encodeURIComponent(event))
    } else if (event.substring(num1).toLowerCase() == ".png" || event.substring(num1).toLowerCase() == ".jpg" || event.substring(num1).toLowerCase() == ".gif" || event.substring(num1).toLowerCase() == ".jpeg") {
        var src = "/sys/file/previewIMGpath?path=" + encodeURIComponent(event);
        var img = new Image();
        var is = true;
        img.onload = function () {//避免图片还未加载完成无法获取到图片的大小。
            //避免图片太大，导致弹出展示超出了网页显示访问，所以图片大于浏览器时下窗口可视区域时，进行等比例缩小。
            var max_height = $(window).height() - 100;
            var max_width = $(window).width();
            //rate1，rate2，rate3 三个比例中取最小的。
            var rate1 = max_height / img.height;
            var rate2 = max_width / img.width;
            var rate3 = 1;
            var rate = Math.min(rate1, rate2, rate3);
            //等比例缩放
            var imgHeight = img.height * rate; //获取图片高度
            var imgWidth = img.width * rate; //获取图片宽度
            var imgHtml = "<img id='img' src='" + src + "' width='" + imgWidth + "px' height='" + imgHeight + "px'/>";
            //弹出层
            layer.open({
                type: 1,
                title: false,//不显示标题
                closeBtn: 1,
                shade: false,
                area: ['auto', 'auto'],
                skin: 'layui-layer-nobg', //没有背景色
                shadeClose: true,
                content: imgHtml,
            });
        };
        img.onerror = function () {
            alert("预览失败，资源可能不存在!")
        };
        img.src = src;
    }
    if (event.substring(num1).toLowerCase() != ".pdf" &&
        event.substring(num1).toLowerCase() != ".xls" && event.substring(num1).toLowerCase() != ".xlsx"
        && event.substring(num1).toLowerCase() != ".doc" && event.substring(num1).toLowerCase() != ".docx"
        && event.substring(num1).toLowerCase() != ".ppt" && event.substring(num1).toLowerCase() != ".pptx"
        && event.substring(num1).toLowerCase() != ".png" &&
        event.substring(num1).toLowerCase() != ".jpg" &&
        event.substring(num1).toLowerCase() != ".gif" &&
        event.substring(num1).toLowerCase() != ".jpeg") {
        alert("该文件暂不支持预览")
    }

};

function isnull(is) {
    if (is == null || is == '' || is == 'null') {
        return ""

    } else {
        return is;
    }
}
function nulls(str) {
    if (str == null || str == '') {
        return  true;
    } else {
        return false;
    }

}
$(function () {


});
